package com.example.demo.controller;

import com.example.demo.config.BaseUserInfo;
import com.example.demo.pojo.Module;
import com.example.demo.pojo.*;
import com.example.demo.service.Impl.*;
import com.example.demo.utils.AliOSSUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class LifebookController {
    @Autowired
    private RoleServiceImpl roleService;
    @Autowired
    private LifebookServiceImpl lifebookService;
    @Autowired
    private HellLogServiceImpl hellLogService;
    @Autowired
    private LogTestServiceImpl logTestService;
    @Autowired
    private YanWangServiceImpl yanWangService;

    //生死簿查询
    @GetMapping("/lifeBookList")
    public Result queryLifeBook(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer limit,
                                String uuid) {
//            Integer i = lifebookService.queryLifeBook01();
        PageInfo list = lifebookService.queryLifeBook(page, limit, uuid);
        return PageResult.success01((int) list.getTotal(), list.getList());
    }

    //生死簿搜索
    @GetMapping("/lifeBookSearch")
    public Result queryLifeBookSearch(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer limit,
                                      Lifebook lifebook) {
        PageInfo list = lifebookService.queryLifeBook02(page, limit, lifebook);
        return PageResult.success01((int) list.getTotal(), list.getList());
    }

    // 生死簿添加
    @PostMapping("/lifeBookAdd")
    public Result addLifeBook(@RequestBody Lifebook lifebook) {
        lifebookService.addLifeBook(lifebook);
        return Result.success("添加成功");
    }

    //生死簿删除
    @DeleteMapping("/lifeBookDelete")
    public Result deleteLifeBook(@RequestParam String id) {
        lifebookService.deleteLifeBook(id);
        return Result.success("删除成功");
    }

    // 生死簿修改
    @PutMapping("/lifeBookUpdate")
    public Result updateLifeBook(@RequestBody Lifebook lifebook) {
        lifebookService.updateLifeBook(lifebook);
        return Result.success("修改成功");
    }

    //图片上传
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/lifeBookUpdateImg")
    public Result upload(@RequestBody MultipartFile photo, String id) throws IOException {
        System.out.println("文件上传,文件名:" + photo.getOriginalFilename());
        //调用阿里云oss工具类进行文件上传
        String upload = aliOSSUtils.upload(photo);
        System.out.println("文件上传完成后，文件访问的url:" + upload);
        lifebookService.updateLifeBookphoto(upload, id);
        return PageResult.success02(upload);
    }
    @PostMapping("/lifeBookInsertImg")
    public Result insertImg(@RequestBody MultipartFile Img) throws IOException {
        String photo = aliOSSUtils.upload(Img);
        return PageResult.success02(photo);
    }

    // GET 首页数据-数量统计
    @GetMapping("/statistics")
    public Result lifebookCount() {
        // 生死簿总数
        int count = lifebookService.queryLifeBook01();
        // 代勾魂数量 人死后还没来得急勾魂的数量
        int count1 = lifebookService.queryLifeBookAfterlife01();

        // 今日入府数 人死后过了头七才入地府
        int coun2 = lifebookService.queryLifeBookAfterlife02();
        // 地狱受刑数
        int coun3 = hellLogService.queryHellLogCount();
        Map<String, Object> map = new HashMap<>();
        map.put("lifebook_count", count);
        map.put("enchant", count1);
        map.put("today_count", coun2);
        map.put("punished", coun3);

        return new PageResult(200, "msgsess", map);
    }

    // 修改生死簿中的数据
    @GetMapping("/asupdate")
    public void xiugai() {
        lifebookService.xiugai();
    }

    // 首页数据-死亡变化
    @GetMapping("/getDeathCount")
    public Result delchange() {
        ArrayList<Map> maps = new ArrayList<>();
        // 获取前n天的死亡数据变化
        LocalDate now = LocalDate.now();
        int count;
        for (int i = 1; i <= 7; i++) {
            HashMap<String, Object> map = new HashMap<>();
            count = lifebookService.delchange(i);
            LocalDate localDate = now.minusDays(i);
            map.put("date", localDate);
            map.put("count", count);
            maps.add(map);
        }
        return new PageResult(200, "成功", maps);
    }

    // 首页数据-轮回变化
    @GetMapping("/getReincarnationCount")
    public Result lunhunchange() {
        ArrayList<Map> maps = new ArrayList<>();
        // 获取前n天的死亡数据变化
        LocalDate now = LocalDate.now();
        int count;
        for (int i = 1; i <= 7; i++) {
            HashMap<String, Object> map = new HashMap<>();
            count = lifebookService.lunhunchange(i);
            LocalDate localDate = now.minusDays(i);
            map.put("date", localDate);
            map.put("count", count);
            maps.add(map);
        }
        return new PageResult(200, "成功", maps);
    }

    //  首页数据-地狱变化
    @GetMapping("/getPunishedCount")
    public Result diyuchange() {
        ArrayList<Map> maps = new ArrayList<>();
        // 获取前n天的死亡数据变化
        LocalDate now = LocalDate.now();
        int count;
        for (int i = 1; i <= 7; i++) {
            HashMap<String, Object> map = new HashMap<>();
            count = lifebookService.diyuchange(i);
            LocalDate localDate = now.minusDays(i);
            map.put("date", localDate);
            map.put("count", count);
            maps.add(map);
        }
        return new PageResult(200, "成功", maps);
    }


    // 获取登录人权限列表
    @GetMapping("/role")
    public Result getuserlevel() {
        String username = BaseUserInfo.get(UserInfoEnu.username.getCode());
        String password = BaseUserInfo.get(UserInfoEnu.password.getCode());
        String module = logTestService.selectLogModule(username, password);
        if (module.equals("all")){
            module=roleService.getlevelAll();
        }
        List<Module> level = roleService.getlevel(module);
        return new PageResult(200, "成功", level);
    }
        // 获取审判结果
        @GetMapping("/getJudgementDetail")
        public Result getJudgementDetail (String uuid){
            YanWang list = yanWangService.getJudgementDetail(uuid);
            return PageResult.success02(list);
        }

        @GetMapping("/getJudgementList")
        public Result getJudgementList (@RequestParam(defaultValue = "1") Integer page,
                @RequestParam(defaultValue = "10") Integer limit,YanWang yanWang){
            PageInfo list = yanWangService.getJudgementList(page, limit,yanWang);
            return PageResult.success01((int) list.getTotal(), list.getList());
        }
        // 审判某人
        @PostMapping("/trial")
        public Result trial (@RequestBody YanWang yanWang){
            String trial = yanWangService.trial(yanWang);
            if (trial.equals("审判成功")){
                return Result.success(trial);
            }else {
                return Result.error(trial);
            }
        }

        // 删除审判记录
        @DeleteMapping("/deleteJudgement")
        public Result deleteJudgement (String uuid){
            yanWangService.deleteJudgement(uuid);
            return Result.success("删除成功");
        }

        // 修改审判结果
        @PutMapping("/updateJudgement")
        public Result updateJudgement (@RequestBody YanWang yanWang){
            yanWangService.updateJudgement(yanWang);
            return Result.success("修改成功");
        }
    }
